const { success, failure } = require("../../utils/responses");
const <%= tableName %>Services = require("../../services/<%= routerPath %>/<%= tableName %>Service");

/**
* 查询<%= title %>列表
* @param req
* @param res
*/
exports.list = async (req, res) => {
try {
// 分页查询
const { count, rows, currentPage, pageSize } =
await <%= tableName %>Services.list(req);
// 返回
success(res, "<%= title %>查询成功", {
lists: rows,
pagination: {
total: count,
currentPage,
pageSize,
},
});
} catch (error) {
failure(res, error);
}
};

/**
* 查询<%= title %>列表
* @param req
* @param res
*/
exports.all = async (req, res) => {
try {
const rows = await <%= tableName %>Services.all(req);
// 返回
success(res, "<%= title %>查询成功", rows);
} catch (error) {
failure(res, error);
}
};

/**
* 查询<%= title %>详情
* @param req
* @param res
*/
exports.detail = async (req, res) => {
try {
const <%= modelName.toLowerCase() %> = await <%= tableName %>Services.getById(req);
success(res, "查询<%= title %>成功", <%= modelName.toLowerCase() %>);
} catch (error) {
failure(res, error);
}
};

/**
* 创建<%= title %>
* @param req
* @param res
*/
exports.add = async (req, res) => {
try {
const body = filterBody(req);
const <%= modelName.toLowerCase() %> = await <%= tableName %>Services.create(body);
success(res, "创建<%= title %>成功", <%= modelName.toLowerCase() %>, 201);
} catch (error) {
failure(res, error);
}
};

/**
* 删除<%= title %>
* @param req
* @param res
*/
exports.del = async (req, res) => {
try {
await <%= tableName %>Services.del(req.body.id);
success(res, "删除<%= title %>成功");
} catch (error) {
failure(res, error);
}
};

/**
* 更新<%= title %>
* @param req
* @param res
*/
exports.update = async (req, res) => {
try {
const body = filterBody(req);
const <%= modelName.toLowerCase() %> = await <%= tableName %>Services.update(req.body.id, body);
success(res, "更新<%= title %>成功", <%= modelName.toLowerCase() %>);
} catch (error) {
failure(res, error);
}
};

/**
* 公共方法过滤白名单
* @param req
*/
function filterBody(req) {
return {
<% for(var i = 0; i < fields.length; i++) { if(fields[i].name !== 'id'){ %>
    <%= fields[i].name %>: req.body.<%= fields[i].name %>, // <%= fields[i].title %>
<% }
} %>
};
}
